package com.moogene.android.engine.animlite;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class DrawableWrapper {
	private String TAG = "DrawableWrapper";

	private Drawable mDrawable;

	private Rect mDest;
	private int mAlpha = 0xFF;
	private ColorMatrixColorFilter mColorMatrixColorFilter;

	private boolean mIsPivotRelative = true;
	private float mRotDegree;
	private float mRotPx;
	private float mRotPy;
	private float mDefaultRotPx;
	private float mDefaultRotPy;
	private static Canvas sCanvas;
	private static boolean sIsRotateLock;
	private static boolean sIsRotateLockStart;

	private int vibrateX;
	private int vibrateY;

	void setBitmap(Bitmap bm) {
		if (bm == null) {
			mDrawable = null;
			return;
		} else {
			mDrawable = new BitmapDrawable(bm);
		}
	}

	void setDefaultRot(int px, int py){
	    mDefaultRotPx = px;
        mDefaultRotPy = py;
        mIsPivotRelative = true;
	}

	void setDest(Rect dest) {
		mDest = dest;
	}

	void setAlpha(int alpha) {
		mAlpha = alpha;
	}

	int getAlpha() {
		return mAlpha;
	}

	void setColorFilter(ColorMatrixColorFilter colorMatrixColorFilter) {
		mColorMatrixColorFilter = colorMatrixColorFilter;
	}

	void setRotatePivot(float px, float py) {
		mRotPx = px;
		mRotPy = py;
		mIsPivotRelative = false;
	}

    void setRelativeRotatePivot(float px, float py) {
        mRotPx = px;
        mRotPy = py;
        mIsPivotRelative = true;
    }

	void setRotateDegree(float degrees){
		mRotDegree = degrees;
	}

	float getRotateDegree(){
		return mRotDegree;
	}

	static void lockRotate() {
		sIsRotateLock = true;
		sIsRotateLockStart = false;
	}

	static void unlockRotate() {
		sIsRotateLock = false;
		if (sCanvas != null) {
			sCanvas.restore();
		}
	}

	void setViabrate(int x, int y){
	    vibrateX = x;
	    vibrateY = y;
	}

	void draw(Canvas canvas) {
		if (mDrawable == null) {
			Log.d(TAG, "No Drawable available, nothing to draw");
			return;
		}
		if (mDest == null) {
			Log.d(TAG, "draw(); dest Rect is null, nothing to draw");
			return;
		}
		int saveCount = -1;
		if (mRotDegree != 0) {
			float px, py;
			if(mIsPivotRelative){
				px = mDefaultRotPx + mDest.left;
				py = mDefaultRotPy + mDest.top;
			}else{
				px = mRotPx;
				py = mRotPy;
			}
			if (!sIsRotateLock) {
				// TODO: optimize the speed by use buffer picture.
				saveCount = canvas.save();
				canvas.rotate(mRotDegree, px, py);
			} else {
				if (!sIsRotateLockStart) {
					sIsRotateLockStart = true;
					sCanvas = canvas;
					canvas.save();
					canvas.rotate(mRotDegree, px, py);
				}
			}
		}
		mDrawable.setAlpha(mAlpha);
		mDrawable.setColorFilter(mColorMatrixColorFilter);
		mDest.offset(vibrateX, vibrateY);
		mDrawable.setBounds(mDest);
		mDrawable.draw(canvas);
		if (saveCount != -1) {
			canvas.restoreToCount(saveCount);
		}
	}
}
